// hyb_yl/doctorHome/pages/task_data/task_data.js
var wxCharts = require("../../../../utils/wxcharts.js");
var windowW = 0;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_state: false,
    status:0,
    page:0,
    type:0,
    wenjuan:[],
    huanjiao:[],
    info:"",
  },
  show_cont: function () {
    this.setData({
      show_state: !this.data.show_state
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 屏幕宽度
    this.setData({
      imageWidth: wx.getSystemInfoSync().windowWidth
    });
    console.log(this.data.imageWidth);

    //计算屏幕宽度比列
    windowW = this.data.imageWidth / 375;
    console.log(windowW);
    var j_id = options.j_id;
    var that = this;
    var id = options.id;
    var zid = options.zid;
    that.setData({
      zid:zid,
      j_id:j_id,
      id:id,
      names:options.names,
      sex:options.sex,
      age:options.age,
      u_thumb:options.u_thumb
    })
    // 获取用户基础信息
    that.getUserinfo();
    // 获取用户测量数据
    that.getUserdata();
    
  },
  // 获取患者基本信息
  getUserinfo:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.userinfo",
      data:{
        j_id:that.data.j_id,
        id:that.data.id
      },
      success:function(res)
      {
        that.setData({
          info:res.data
        })
      }
    })
  },
  // 获取所有问卷
  getUserdata:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.user_data",
      data:{
        j_id:that.data.j_id,
        f_id:that.data.id,
        zid:that.data.zid,
        type:that.data.type
      },
      success:function(res)
      {
        if(that.data.type == 1 || that.data.type == 2)
        {
          that.setData({
            data:res.data,
          })
        }else{
          var date = res.data.date;
          var height = res.data.height;
          var low = res.data.low;
          var heart_rate = res.data.heart_rate
          var sugar = res.data.sugar;
          var cholesterol = res.data.cholesterol;
          var temperature = res.data.temperature;
          var uricacid = res.data.uricacid;
          try {
            var res = wx.getSystemInfoSync();
            var windowWidth = res.windowWidth;
            var windowHeight = res.windowHeight * 0.3;
          } catch (e) {
            console.error("系统信息错误")
          };
          new wxCharts({ //当月用电折线图配置
            canvasId: 'xueya',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '心率（次/分）',
              data: heart_rate,
              color: '#0087e0',
              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            }, {
              name: '高压（收缩压）',
              data: height,
              color: '#ff7272',

              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            },
            {
              name: '低压（舒张压）',
              data: low,
              color: '#4dbb99',
              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 200,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
          new wxCharts({ //当月用电折线图配置
            canvasId: 'xuetang',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '血糖值',
              data: sugar,
              color:'#ff7272',
              format: function (val, name) {
                return val.toFixed(2) + 'kWh';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 20,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
          new wxCharts({ //当月用电折线图配置
            canvasId: 'wendu',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '温度',
              data: temperature,
              color:'#ff7272',
              format: function (val, name) {
                return val.toFixed(2) + '℃';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 20,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
          new wxCharts({ //当月用电折线图配置
            canvasId: 'dangu',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '胆固醇',
              data: cholesterol,
              color:'#ff7272',
              format: function (val, name) {
                return val.toFixed(2) + 'mmol/L';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 20,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
          new wxCharts({ //当月用电折线图配置
            canvasId: 'niaos',
            type: 'line',
            categories: date, //模拟的x轴横坐标参数
            animation: true,
            // background: '#f5f5f5',
            series: [{
              name: '尿酸',
              data: uricacid,
              color:'#ff7272',
              format: function (val, name) {
                return val.toFixed(2) + 'μmol/L';
              }
            }],
            xAxis: {
              disableGrid: true
            },
            yAxis: {
              title: '',
              format: function (val) {
                return val.toFixed(2);
              },
              max: 20,
              min: 0
            },
            width: windowWidth,
            height: windowHeight,
            dataLabel: false,
            dataPointShape: true,
            extra: {
              lineStyle: 'Broken'
            }
          });
        }
        
      }
    })
  },
  changestatus:function(e)
  {
    this.setData({
      type:e.currentTarget.dataset.type
    })
    this.getUserdata();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    // pieCanvas
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var status = this.data.status;
    if(status == 0)
    {
      this.getuserData();
    }else if(status == 1)
    {
      this.getUserWenjuan();
    }else if(status == 2)
    {
      this.getUserHuanjiao();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})